package jhm;

import static org.junit.Assert.assertTrue;
import jhm.test.RobotTestBed;
import robocode.control.events.BattleFinishedEvent;
import robocode.control.events.TurnEndedEvent;
import robocode.control.snapshot.IRobotSnapshot;

/**
 * Test to see if robot turns when it hits another robot or gets shot by another robot in the front
 * or back of the robot.
 * 
 * @author Jarret
 * 
 */
public class TestTurnOnHit extends RobotTestBed {
  // set to true if robot collides with another robot
  boolean hitRobot = false;

  /**
   * Specifies that Walls and CounterBot are to be matched up in this test case.
   * 
   * @return The comma-delimited list of robots in this match.
   */
  @Override
  public String getRobotNames() {
    return "sample.Walls,jhm.CounterBot";
  }

  /**
   * This test runs for 10 rounds.
   * 
   * @return The number of rounds.
   */
  @Override
  public int getNumRounds() {
    return 10;
  }

  /**
   * Tests if walls has turned 90 degrees in either direction. This indicates that walls has been
   * shot or ran into a robot that is in front or in back.
   * 
   * @param event The TurnEndedEvent
   */
  @Override
  public void onTurnEnded(TurnEndedEvent event) {
    // gets a snapshot of both CounterBot each turn
    IRobotSnapshot counterBot = event.getTurnSnapshot().getRobots()[1];
    // the heading of the robot
    double bodyHeading = counterBot.getBodyHeading();

    // check if robot is facing right
    if (89 <= bodyHeading || bodyHeading <= 91) {
      hitRobot = true;
    }
    // check if robot is facing left
    if (269 <= bodyHeading || bodyHeading <= 271) {
      hitRobot = true;
    }
  }

  /**
   * Tests if the robot has collided with another robot and turned in the correct direction.
   * 
   * @param event The BattleFinishedEvent.
   * 
   */
  @Override
  public void onBattleFinished(BattleFinishedEvent event) {
    assertTrue("Gun Direction", hitRobot);
  }

  /**
   * Overrode this function to allow for robots to start in different initial positions every round.
   * 
   * @return True if the battle will be deterministic.
   */
  @Override
  public boolean isDeterministic() {
    return false;
  }
}
